#include <amxmodx>
#include <amxmisc>
#include <engine>

new PLUGIN[] = "KeyCard"
new AUTHOR[] = "Xide"
new VERSION[] = "1.0"

public plugin_init()
{
	register_plugin(PLUGIN, VERSION, AUTHOR)
	
	register_srvcmd("item_keycard","cmdKeyCard")
}

// item_keycard <id> "doorname"
public cmdKeyCard()
{
	new playerID, doorname[64], arg1[32], entID, entBody, classname[32], doorname1[64]
	read_argv(1,arg1,sizeof(arg1))
	read_argv(2,doorname,sizeof(doorname))
	
	playerID = str_to_num(arg1)
	
	if(!is_user_alive(playerID)) return PLUGIN_HANDLED
	
	get_user_aiming(playerID,entID,entBody,200)
	
	entity_get_string(entID,EV_SZ_classname,classname,sizeof(classname))
	entity_get_string(entID,EV_SZ_target,doorname1,sizeof(doorname1))
	
	if(!equali(classname,"func_door_rotating") || !equali(classname,"func_door"))
	{
		client_print(playerID,print_chat,"[%s] You must be looking at a door",PLUGIN)
		return PLUGIN_HANDLED
	}
	
	if(doorname1[playerID] == doorname[playerID])
	{
		force_use(playerID,entID)
		fake_touch(entID,playerID)
		client_print(playerID,print_chat,"[%s] You insert the Key Card, and open the door",PLUGIN)
		return PLUGIN_HANDLED
	}
	else
	{
		client_print(playerID,print_chat,"[%s] You intert the Key Card, however the door denies you access",PLUGIN)
		return PLUGIN_HANDLED
	}
	return PLUGIN_HANDLED
}